﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WarehouseManager.Core;
using WarehouseManager.Model;

namespace WarehouseManager.DataAccess
{
    
    class HanghoaAccess
    {
        public HanghoaAccess() { }
        private dbConnection dbCon = new dbConnection();
        public DataView timkiemMahh(string maHH)
        {
            DataView dataView = layToanbohanghoa().DefaultView;
            dataView.RowFilter = string.Format("maHH like '%{0}%'", maHH);
            return dataView;
        }
        //---- Tìm kiếm theo tên học sinh
        public DataView timkiemTenhh(string tenHH)
        {
            DataView dataView = layToanbohanghoa().DefaultView;
            dataView.RowFilter = string.Format("tenHH like '%{0}%'", tenHH);
            return dataView;
        }
        //---- Tim Học sinh theo ma va tra ve mot bang
        public DataTable timhanghoatheoma(string mahh)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maHH", SqlDbType.NVarChar, 10);
            ma.Value = mahh;
            listParam[0] = ma;
            return dbCon.executeSelectProcedureQuery("proc_select_IDHangHoa", listParam);
        }
        //---- Thêm Hàng hóa
        public int themhanghoa(HangHoa hh)
        {
            if (timhanghoatheoma(hh.maHH1).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.maHH1;
                listParam[0] = mahh;
                listParam[1] = new SqlParameter("@tenHH", hh.tenHH1);
                listParam[2] = new SqlParameter("@donGia", hh.donGia1);
                listParam[3] = new SqlParameter("@donViTinh", hh.donViTinh1);
                listParam[4] = new SqlParameter("@nhaSX", hh.nhaSX1);
                listParam[5] = new SqlParameter("@soLuong", hh.soLuong1);
                
                dbCon.executeInsertProcedureQuery("proc_insertHangHoa", listParam);
                return 1;
            }
        }
        //---- Sửa Hàng hóa
        public int suahanghoa(HangHoa hh)
        {
            if (timhanghoatheoma(hh.maHH1) == null) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.maHH1;
                listParam[0] = mahh;
                listParam[1] = new SqlParameter("@tenHH", hh.tenHH1);
                listParam[2] = new SqlParameter("@donGia", hh.donGia1);
                listParam[3] = new SqlParameter("@donViTinh", hh.donViTinh1);
                listParam[4] = new SqlParameter("@nhaSX", hh.nhaSX1);
                listParam[5] = new SqlParameter("@soLuong", hh.soLuong1);
                dbCon.executeUpdateProcedureQuery("proc_updateHangHoa", listParam);
                return 1;
            }
        }
        //----- Xóa Hàng hóa
        public int xoahanghoa(HangHoa hh)
        {
            if (timhanghoatheoma(hh.maHH1) == null) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                SqlParameter mahh = new SqlParameter("@maHH", SqlDbType.VarChar, 10);
                mahh.Value = hh.maHH1;
                listParam[0] = mahh;
                dbCon.executeDeleteProcedureQuery("proc_deleteHangHoa", listParam);
                return 1;
            }
        }
        //---- Lấy toàn bộ Hàng hóa
        public DataTable layToanbohanghoa()
        {
            return dbCon.executeSelectProcedureQueryNonParameter("proc_selectHangHoa");
        }
        public DataTable layMahanghoa()
        {
            return dbCon.executeSelectProcedureQueryNonParameter("proc_selectMaHangHoa");
        }
    }
}
